//
//  QLMNoticeListVC.m
//  Xiaomakuaixiu
//
//  Created by 龚碧容 on 2017/11/4.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "QLMNoticeListVC.h"
#import "QLMNoticeTabCell.h"
#import "QLMDetailAddressVC.h"

@interface QLMNoticeListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *noticetabview;
@end

@implementation QLMNoticeListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title=@"小马公告";
    
    _noticetabview=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _noticetabview.backgroundColor=[UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1.0];
    _noticetabview.delegate=self;
    _noticetabview.dataSource=self;
    _noticetabview.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    _noticetabview.showsVerticalScrollIndicator=NO;
    _noticetabview.showsHorizontalScrollIndicator=NO;
    _noticetabview.tableFooterView=[UIView new];
    [self.view addSubview:_noticetabview];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=NO;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - UITableView datasource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView=[[UIView alloc] init];
    headerView.backgroundColor=[UIColor whiteColor];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView=[[UIView alloc] init];
    footerView.backgroundColor=[UIColor clearColor];
    return footerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _arrayModel.dataAnnouncement.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50+35;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QLMNoticeTabCell *cell=[QLMNoticeTabCell cellWithTableView:tableView];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    _announcementModel=[[QLMAnnouncementModel alloc] initWithDictionary:_arrayModel.dataAnnouncement[indexPath.section] error:nil];

    cell.noticeTitleLabel.text=[NSString stringWithFormat:@"%@",_announcementModel.announcementTitle];
    cell.noticeDateLabel.text=[self timeWithTimeIntervalString:_announcementModel.pubdate];

    return cell;
}

#pragma mark - UITableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    QLMAnnouncementModel *mentModel=[[QLMAnnouncementModel alloc] initWithDictionary:_arrayModel.dataAnnouncement[indexPath.section] error:nil];

    QLMDetailAddressVC *webVC=[QLMDetailAddressVC new];
    webVC.htmlurlStr=[NSString stringWithFormat:@"%@/html/phone/announcementDetails.html?id=%@",kBaseURL,mentModel.announcementId];
    webVC.title=@"小马公告";
    self.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:webVC animated:YES];
}

- (NSString *)timeWithTimeIntervalString:(NSString *)timeString{
    // 格式化时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/1000.0];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}

@end
